<?php
/**
 * (logic) 报告解析失败（未解析）
 */

namespace App\Logic\MonthlyReportLT;

use App\Models\MonthlyReportLT\Tmp\TmpReFee;
use App\Models\Product\ProductAmazonSku;
use App\Models\MonthlyReportLT\Tmp\TmpSales;
use App\Models\MonthlyReportLT\Tmp\TmpThFee;

use App\Models\Product\ProductSku;
use Illuminate\Support\Facades\DB;
class ParseMiss
{
    public static function parse($action)
    {
        self::{$action.'Exe'}();
    }

    private static function saExe(){
        $existPas = [];
        $existMiss = [];
        TmpSales::query()
            ->select('sku')
            ->distinct()
            ->orderBy('sku')
            ->chunk(500, function ($items) use (&$existPas,&$existMiss){
                $miss = [];
                $time = date('Y-m-d H:i:s');
                foreach ($items as $item){
                    if(!$item->sku)continue;
                    $count = DB::table('lt_mr_parse_miss')
                        ->where('field',$item->sku)
                        ->where('sour','sa')
                        ->count();
                    if($count > 0)continue;
                    if(!array_key_exists($item->sku,$existPas)){
                        $count = ProductSku::query()
                            ->where('psku_code',$item->sku)
                            ->count();
                        $existPas[$item->sku] = $count;
                    }
                    if($existPas[$item->sku] == 0){
                        if(!in_array($item->sku,$existMiss)){
                            $existMiss[] = $item->sku;
                            $temp = [];
                            $temp['field'] = $item->sku;
                            $temp['desc'] = "系统未匹配到该的产品";
                            $temp['sour'] = 'sa';
                            $temp['created_at'] = $time;
                            $temp['updated_at'] = $time;
                            $miss[] = $temp;
                        }
                    }
                }
                if(!empty($miss)){
                    try{
                        DB::beginTransaction();
                        DB::table('lt_mr_parse_miss')->insert($miss);
                        DB::commit();
                    }catch (\Exception $e){
                        DB::rollBack();
                        report($e);
                    }
                }
            });
    }

    private static function reExe(){
        $existPas = [];
        $existMiss = [];
        TmpReFee::query()
            ->select('sku')
            ->distinct()
            ->orderBy('sku')
            ->chunk(500, function ($items) use (&$existPas,&$existMiss){
                $miss = [];
                $time = date('Y-m-d H:i:s');
                foreach ($items as $item){
                    $count = DB::table('lt_mr_parse_miss')
                        ->where('field',$item->sku)
                        ->where('sour','re')
                        ->count();
                    if($count > 0)continue;
                    if(!array_key_exists($item->sku,$existPas)){
                        $count = ProductSku::query()
                            ->where('psku_code',$item->sku)
                            ->count();
                        $existPas[$item->sku] = $count;
                    }
                    if($existPas[$item->sku] == 0){
                        if(!in_array($item->sku,$existMiss)){
                            $existMiss[] = $item->sku;
                            $temp = [];
                            $temp['field'] = $item->sku;
                            $temp['desc'] = "系统未匹配到该的产品";
                            $temp['sour'] = 're';
                            $temp['created_at'] = $time;
                            $temp['updated_at'] = $time;
                            $miss[] = $temp;
                        }
                    }
                }
                if(!empty($miss)){
                    try{
                        DB::beginTransaction();
                        DB::table('lt_mr_parse_miss')->insert($miss);
                        DB::commit();
                    }catch (\Exception $e){
                        DB::rollBack();
                        report($e);
                    }
                }
            });
    }

    private static function thExe(){
        $existPas = [];
        $existMiss = [];
        $existRas = [];
        TmpThFee::query()
            ->select('sku')
            ->distinct()
            ->orderBy('sku')
            ->chunk(500, function ($items) use (&$existPas,&$existMiss,&$existRas){
                $miss = [];
                $time = date('Y-m-d H:i:s');
                foreach ($items as $item){
                    $count = DB::table('lt_mr_parse_miss')
                        ->where('field',$item->sku)
                        ->where('sour','th')
                        ->count();
                    if($count > 0)continue;
                    if(!array_key_exists($item->sku,$existPas)){
                        $existPas[$item->sku] = ProductAmazonSku::getValueByCode($item->sku,'psku_id');
                    }
                    if(!$existPas[$item->sku]){
                        if(!in_array($item->sku,$existMiss)){
                            $existMiss[] = $item->sku;
                            $temp = [];
                            $temp['field'] = $item->sku;
                            $temp['desc'] = "系统未匹配到该产品-亚马逊产品列表";
                            $temp['sour'] = 'th';
                            $temp['created_at'] = $time;
                            $temp['updated_at'] = $time;
                            $miss[] = $temp;
                        }
                    }
                }
                if(!empty($miss)){
                    try{
                        DB::beginTransaction();
                        DB::table('lt_mr_parse_miss')->insert($miss);
                        DB::commit();
                    }catch (\Exception $e){
                        DB::rollBack();
                        report($e);
                    }
                }
            });
    }
}
